/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modulo1;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.*;
import java.io.PrintWriter;
import Modelo.DAL.PreReporte_DAO;
import Modelo.PreReporte;
import Modelo.ComunidadUniversitaria;
import Acceso.Security;


/**
 *
 * @author miguel474
 */
public class listaPreReportes extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador", "Recibir Pre-Reportes"};
        if (!Security.validateSession(request, a)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        String ci_den = request.getParameter("ci_den");
        String nombre_den = request.getParameter("nombre_den");
        String apellido_den = request.getParameter("apellido_den");
        String nombre_rec = request.getParameter("nombre_rec");
        String apellido_rec = request.getParameter("apellido_rec");
        String ubicacion = request.getParameter("ubicacion");


        String limit = request.getParameter("limit");
        String offset = request.getParameter("offset");
        String order = request.getParameter("order");
        String asc = request.getParameter("asc");

        System.out.println("**** ci_den ("+ci_den+"), nombre_den ("+nombre_den+"), apellido_den ("+apellido_den+"), nombre_rec ("+nombre_rec+"), apellido_rec ("+apellido_rec+"), ubicacion ("+ubicacion+")");
        ComunidadUniversitaria cu1 = new ComunidadUniversitaria();
        cu1.setCi(ci_den);
        cu1.setNombre1(nombre_den);
        cu1.setApellido1(apellido_den);

        ComunidadUniversitaria cu2 = new ComunidadUniversitaria();
        cu2.setNombre1(nombre_rec);
        cu2.setApellido1(apellido_rec);

        PreReporte pr = new PreReporte();
        pr.setUbicacion(ubicacion);

        System.out.println("cici " + cu1.getCi());
        response.setContentType("text/json");

        JSONObject preR = PreReporte_DAO.consultarPreReportes(pr, cu1, cu2, limit, offset, order, asc);

        PrintWriter pw = response.getWriter();
        pw.write(preR.toString());
        pw.flush();
        pw.close();
        

        return mapping.findForward(SUCCESS);
    }

}
